package com.um.jdy.app.shop.activity.impl;

import cn.hutool.core.util.NumberUtil;
import com.um.jdy.app.shop.activity.Activity;
import com.um.jdy.app.shop.activity.BaseActivity;
import com.um.jdy.app.shop.entity.bo.ActivityBO;
import com.um.jdy.app.shop.entity.bo.CouponsBO;
import com.um.jdy.app.shop.entity.bo.FullReduceGiftGoodsBO;
import com.um.jdy.app.shop.entity.bo.GoodsBO;
import com.um.jdy.common.market.entity.po.ActivityGoods;
import com.um.jdy.common.market.entity.po.ActivityGoodsOption;
import com.um.jdy.common.market.service.ActivityGoodsOptionService;
import com.um.jdy.common.market.service.ActivityGoodsService;
import com.um.springboot.starter.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 拼团活动
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2021/6/28 4:34 下午
 */
@Service
@Transactional
public class GroupActivityImpl extends BaseActivity implements Activity {

    @Autowired
    ActivityGoodsService activityGoodsService;

    @Autowired
    ActivityGoodsOptionService activityGoodsOptionService;

    /**
     * 保存活动对象
     *
     * @param activityBO
     */
    @Override
    public void setActivityEntity(ActivityBO activityBO) {
        this.activityBO = activityBO;
    }

    /**
     * 获取活动实体
     *
     * @return
     */
    @Override
    public ActivityBO getActivityEntity() {
        return this.activityBO;
    }

    /**
     * 是否满足活动条件
     *
     * @return
     */
    @Override
    public boolean isMeet(List<GoodsBO> goodsBOList) {
        boolean isMeet = false;
        List activityGoodsList = new ArrayList();
        Integer fee = 0;

        //当前活动的商品列表
        List<ActivityGoods> currentActivityGoodsList = activityGoodsService.getListBySqlName("get_activity_goods_list",new Object[]{this.activityBO.getMarketActivityId(),activityGoodsService.getComId()});

        for(GoodsBO goodsBO : goodsBOList){

            for(ActivityGoods currentActivityGoods : currentActivityGoodsList){
                if(StringUtil.equals(currentActivityGoods.getShopGoodsId(), goodsBO.getShopGoodsId())){

                    goodsBO.setMarketActivityId(this.activityBO.getMarketActivityId());
                    goodsBO.setActivityType(this.activityBO.getType());

                    ActivityGoodsOption activityOption = activityGoodsOptionService.getEntity(new String[]{"market_activity_goods_id", "shop_goods_option_id"}, new Object[]{currentActivityGoods.getMarketActivityGoodsId(), goodsBO.getShopGoodsOptionId()});
                    if(null == activityOption || activityOption.getActivityStock() < goodsBO.getNum()){
                        continue;
                    }

                    //计算活动价格
                    Integer activityPrice = activityOption.getActivityPrice();
                    Integer totalPrice = NumberUtil.mul(goodsBO.getNum(),activityPrice).intValue();
                    fee = NumberUtil.add(fee,NumberUtil.sub(goodsBO.getTotalPrice(),totalPrice)).intValue();
                    goodsBO.setActivityPrice(activityPrice);
                    goodsBO.setTotalPrice(totalPrice);
                    goodsBO.setPayment(totalPrice);
                    goodsBO.setGroupNum(activityOption.getGroupNum());
                    goodsBO.setMarketActivityGoodsId(activityOption.getMarketActivityGoodsId());
                    goodsBO.setMarketActivityGoodsOptionId(activityOption.getMarketActivityGoodsOptionId());

                    activityGoodsList.add(goodsBO);
                    isMeet = true;
                }
            }

        }

        this.activityGoodsList = activityGoodsList;
        this.fee = fee;

        return isMeet;
    }

    /**
     * 返回参与活动的商品列表及活动价格
     *
     * @return
     */
    @Override
    public List<GoodsBO> getActivityGoods() {
        return this.activityGoodsList;
    }

    /**
     * 返回赠品列表（满赠活动专用）
     *
     * @return
     */
    @Override
    public FullReduceGiftGoodsBO getGiftGoods() {
        return null;
    }

    /**
     * 返回赠优惠券列表（满赠活动专用）
     *
     * @return
     */
    @Override
    public CouponsBO getGiftCoupons() {
        return this.fullReduceGiftCouponsBO;
    }

    /**
     * 返回优惠收费说明项
     *
     * @return
     */
    @Override
    public Integer getFee() {
        return this.fee;
    }
}
